/*

	Fortune For Android - By camperdave!

Copyright (c) 2010, Dave Lee
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



*/


/*
* Based on: Fefe's fortune(6), which is Public domain.
* Developed under the One True OS, Linux.
* Will output a random fortune to stdout.  Does not need strfile(1).
* Will utilize system's /dev/urandom if available.
* Uses mmap(2) for ease and performance.
*
* Usage: fortune <filename1> [<filename2>...]
*/

/* Added RE matching capability, getopt handling.
* David Frey, Oct 1998
*/

/* [20010625] rip stdio and locale crap */

#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
/* #include <stdio.h> */
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* #include <locale.h> */
/* #include <regex.h> */
#include <getopt.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>

void carp(const char* msg,const char* filename) {
	const char* err=strerror(errno);
	write(2,msg,strlen(msg));
	if (filename) {
		write(2," `",2);
		write(2,filename,strlen(filename));
		write(2,"': ",3);
		write(2,err,strlen(err));
	}
	write(2,".\n",2);
}

void die(const char* msg,const char* filename) {
	carp(msg,filename);
	exit(1);
}

#ifndef _POSIX_MAPPED_FILES
#warning Your system headers say that mmap is not supported!
#endif

#define VERSION "1.0"
#define FORTUNEDIR "/sdcard/games/fortunes"

struct option const long_options[] =
{
	{"help",     no_argument,       0, 'h'},
	{"help",     no_argument,       0, '?'},
	{"match",    required_argument, 0, 'm'},
	{"version",  no_argument,       0, 'V'},
	{(char *)0,  0,                 0, (char)0}
};

const char *progname;

char *printquote(char *map, char *c, size_t len)
{
	const char* start;
	while (c > map &&
	(*c != '%' || (c<map+len && *(c+1) != '\n') || *(c-1) != '\n'))
	c--;
	if (*c == '%')  c++;
	if (*c == '\n') c++;
	start=c;
	while (c < map + len &&
	(*c != '%' || (c<map+len && *(c+1) != '\n') || *(c-1) != '\n'))
	c++;
	write(1,start,c-start);

	return c;
}


int printFortune(char *filename)
/* Search FILENAME for a given RE (if != NULL) or print out a
	random fortune (if RE == NULL)
	*/
{
	int fd, len;
	int retcode = 1;
	char *map;

	fd = open(filename, 0);
	if (fd >= 0) {
		len = lseek(fd, 0, SEEK_END);
		map = (char *)mmap(0, len, PROT_READ, MAP_SHARED, fd, 0);
		if (map){
			int ofs;
			ofs = rand() % len;
			printquote(map, map + ofs, len);
			retcode = 0;
		} else {
			die("could not mmap",filename);
#if 0
			fprintf(stderr, "%s: could not mmap `%s'!\n", progname, filename);
#endif
		}
		munmap(map, len);
		close(fd);
	} else {
		die("could not open",filename);
#if 0
		fprintf(stderr, "%s: could not open `%s'!\n", progname, filename);
#endif
	}

	return retcode;
}

int main(int argc, char *argv[])
{
	int fd;
	int retcode;
	int c;

	progname=argv[0];
	while ((c = getopt_long(argc, argv, "h?:V",
	long_options, (int *) 0)) != -1) {
		switch (c) {
		case 0  : break;
		case 'h':
		case '?': carp("Usage: fortune [-hV] [filename...]",0);
			return 0;
		case 'V': carp("fortune " VERSION "\n",0);
			return 0;
			default : break;
		}
	}

#if 0
	setlocale(LC_CTYPE, "");
#endif

	if ((fd = open("/dev/urandom", O_NDELAY)) >= 0) {
		int foo;
		if (read(fd, &foo, sizeof(int)) > 0)
		srand(foo);
		else
		srand(time(0));
	} else
	srand(time(0));

	/* Were some filenames given as arguments? */
	if (argc > optind) {
		char *filename;

		filename = argv[(rand() % (argc - optind)) + optind];

		retcode = printFortune(filename);
	} else {
		/* No? Then search the fortune directory. */
		DIR *d;

		d=opendir(FORTUNEDIR);
		if (d == NULL) {
			carp("could not open directory",FORTUNEDIR);
			retcode=1;
		} else {
			struct dirent *e;
			char *files=NULL;
			int fileno=0;

			retcode=0;

			chdir(FORTUNEDIR);
			while ((e=readdir(d)) != NULL) {
				struct stat sbuf;

				if (lstat(e->d_name, &sbuf) < 0) {
					carp("could not stat",e->d_name);
				} else {
					if (S_ISREG(sbuf.st_mode)) {
						
						files=(char *)realloc(files,(fileno+1)*NAME_MAX);
						strcpy(&files[fileno*NAME_MAX],e->d_name);
						fileno++;
						
					}
				}
			}

			closedir(d);
			
			printFortune(&files[(rand() % fileno)*NAME_MAX]);
			free(files);
		}
	}
	return retcode;
}

